﻿using Chain.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Mobile_New_SubmitPay : System.Web.UI.Page
{
    Chain.BLL.MicroWebsiteOrderLogDetail MicroWebsiteOrderLogDetailBll = new Chain.BLL.MicroWebsiteOrderLogDetail();
    Chain.BLL.MicroWebsiteOrderLog orderBll = new Chain.BLL.MicroWebsiteOrderLog();
    Chain.BLL.Goods GoodsBll = new Chain.BLL.Goods();
    private decimal TotalSubtotal;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
           
           
                this.msg.Visible = false;
                Chain.BLL.Mem memBll = new Mem();
                HttpCookie wxMem = base.Request.Cookies["uid"];
                DataTable dt = memBll.GetList(string.Format(" memid='{0}'", wxMem.Value)).Tables[0];
                this.rpOrder.DataSource = dt;
                this.rpOrder.DataBind();

                HttpCookie cookie = base.Request.Cookies["goodlist"];
                if ((cookie != null) && !string.IsNullOrEmpty(cookie.Value))
                {
                    this.BindGoodsinventory(cookie);
                    this.zhifu_price2.InnerText = this.TotalSubtotal.ToString("0.00");
                }
            
        }
        catch {
            this.alls.Visible = false;
            this.msg.Visible = true;
        }
    }


    private void BindGoodsinventory(HttpCookie cookie)
    {
        Goods MicroWebsiteGoodsBll = new Goods();
        StringBuilder sbWhere = new StringBuilder("a.GoodsID in(");

        string[] keys = HttpUtility.UrlDecode(cookie.Value).Trim().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
        List<string> List = new List<string>();
        foreach (string item in keys)
        {

            int GiftID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
            List.Add(item);
            sbWhere.AppendFormat("{0},", GiftID);


        }

        sbWhere.Append("0)");
        DataTable dt = MicroWebsiteGoodsBll.GetOKGoods(sbWhere.ToString()).Tables[0];
        dt = this.UpDateTable(dt, List);
        this.rpOrderDetail.DataSource = dt;
        this.rpOrderDetail.DataBind();
        HttpCookie wxMem = base.Request.Cookies["uid"];
        Chain.BLL.Mem memBll = new Mem();
        string MemMoney = memBll.GetList(string.Format(" MemID='{0}'", wxMem.Value)).Tables[0].Rows[0]["MemMoney"].ToString();
        if (!string.IsNullOrEmpty(MemMoney) && MemMoney != "")
        {
            this.my_yue.InnerHtml = Convert.ToDecimal(MemMoney).ToString("0.00");
        }
    }
    private DataTable UpDateTable(DataTable dt, List<string> keys)
    {
        dt.Columns.Add("ConvertNumber");
        dt.Columns.Add("Subtotal");
        foreach (string item in keys)
        {
            int MicroGoodsID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
            int ConvertNumber = Convert.ToInt32(item.Split(new char[] { ',' })[2]);
            DataRow row = dt.Select("GoodsID=" + MicroGoodsID)[0];
            row["ConvertNumber"] = ConvertNumber;
            row["Subtotal"] = ConvertNumber * Convert.ToDecimal(row["Price"]);
            this.TotalSubtotal += Convert.ToDecimal(row["Subtotal"]);
        }
        return dt;
    }

    /// <summary>
    /// 商品详情展示
    /// </summary>
    /// <param name="dt"></param>
    /// <param name="keys"></param>
    /// <returns></returns>
    private DataTable UpDateTable(DataTable dt, string[] keys)
    {
        try
        {
            dt.Columns.Add("ConvertNumber");
            dt.Columns.Add("Subtotal");
            foreach (string item in keys)
            {
                int MicroGoodsID = Convert.ToInt32(item.Split(new char[] { ',' })[0]);
                int ConvertNumber = Convert.ToInt32(item.Split(new char[] { ',' })[1]);
                DataRow row = dt.Select("GoodsID=" + MicroGoodsID)[0];
                row["ConvertNumber"] = ConvertNumber;
                row["Subtotal"] = ConvertNumber * Convert.ToDecimal(row["Price"]);
                this.TotalSubtotal += Convert.ToDecimal(row["Subtotal"]);
            }

            return dt;
        }
        catch (Exception)
        {
            return dt;
            throw;
        }
    }




}